from django.utils.translation import gettext_lazy

from rest_framework.serializers import Serializer
from rest_framework.response import Response
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi

from apps.api.viewsets import NormalGenericViewSet
from apps.app_auth_provider.models import AuthProvider
from apps.app_auth_provider.serializers import AuthProviderSerializer


class AuthProviderViewSet(NormalGenericViewSet):
    permission_classes = []
    pagination_class = None
    lookup_field = 'id'

    @swagger_auto_schema(
        tags=['auth_provider'],
        operation_summary=gettext_lazy('列举认证服务提供者'),
        manual_parameters=[
            openapi.Parameter(
                name='is_active',
                in_=openapi.IN_QUERY,
                type=openapi.TYPE_BOOLEAN,
                required=False,
                description=gettext_lazy('可用状态')
            ),
        ],
        responses={
            200: ''''''
        }
    )
    def list(self, request, *args, **kwargs):
        """
        列举认证服务提供者

            http code 200:
            {
                "providers": [
                    {
                        "id": "0691435a-9837-7cf7-8000-b4a5159af1bc",
                        "name": "ORCID认证服务",
                        "provider_type": "orcid",
                        "is_active": true,  # 可用状态
                        "created_at": "2025-11-12T07:22:17.515710Z"
                    }
                ]
            }
        """
        is_active = request.query_params.get('is_active', None)

        if is_active is None:
            providers = AuthProvider.objects.all().order_by('created_at')
        else:
            is_active = is_active.lower() == 'true'
            providers = AuthProvider.objects.filter(is_active=is_active).order_by('created_at')

        return Response(data={
            'providers': AuthProviderSerializer(providers, many=True).data,
        })

    def get_serializer_class(self):
        return Serializer
